package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.form.ContractForm;
import com.yxinsur.imc.business.service.IImcContractService;
import com.yxinsur.imc.business.vo.ContractVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.exception.BusinessException;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 协议管理 Controller
 *
 * @author imc
 * @date 2021-04-21
 */
@Api(value = "6.协议管理模块接口服务", tags = {"6.协议管理模块接口服务"})
@RestController
@RequestMapping("/business/contract")
public class ImcContractController extends BaseController {
    @Autowired
    private IImcContractService imcContractService;

    /**
     * 协议管理-查询列表
     */
    @ApiOperation("协议管理-查询列表")
    @GetMapping("/list")
    public TableDataInfo list(ContractForm contractForm) {
        Date contractStartDate = contractForm.getContractStartDate();
        Date contractEndDate = contractForm.getContractEndDate();
        if (contractStartDate != null && contractEndDate != null && contractEndDate.compareTo(contractStartDate) < 0) {
            throw new BusinessException("截止日期不能小于开始日期");
        }
        startPage();
        String authOrgIds = SecurityUtils.getLoginUser().getAuthOrgIds();
        String[] split = authOrgIds.split(",");
        List<Long> orgIds = Arrays.stream(split).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] orgIdArr = new Long[orgIds.size()];
        orgIds.toArray(orgIdArr);
        contractForm.setOrgIds(orgIdArr);
        List<ContractVo> list = imcContractService.selectImcContractList(contractForm);
        return getDataTable(list);
    }

    /**
     * 查询主协议列表
     */
    @ApiOperation("查询主协议列表")
    @GetMapping("/list/master")
    public AjaxResult listMasterContract(ContractForm contractForm) {
        return AjaxResult.success(this.imcContractService.listMasterContract(contractForm.getId(), contractForm.getRepoSupplierId()));
    }

    /**
     * 协议管理-查询详情
     */
    @ApiOperation("协议管理-查询详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imcContractService.selectImcContractById(id));
    }

    /**
     * 协议管理-新增协议
     */
    @ApiOperation("协议管理-新增协议")
    @Log(title = "协议管理-新增协议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ContractForm contractForm) {
        return this.imcContractService.insertImcContract(contractForm);
    }

    /**
     * 修改协议
     */
    @ApiOperation("修改协议")
    @Log(title = "修改协议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ContractForm contractForm) {
        return this.imcContractService.updateImcContract(contractForm);
    }

    /**
     * 删除协议
     */
    @ApiOperation("删除协议")
    @Log(title = "删除协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return this.imcContractService.deleteImcContractById(id);
    }

    /**
     * 导出协议
     */
    @ApiOperation("导出协议")
    @Log(title = "导出协议", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ContractForm contractForm) {
        String authOrgIds = SecurityUtils.getLoginUser().getAuthOrgIds();
        String[] split = authOrgIds.split(",");
        List<Long> orgIds = Arrays.stream(split).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] orgIdArr = new Long[orgIds.size()];
        orgIds.toArray(orgIdArr);
        contractForm.setOrgIds(orgIdArr);

        List<ContractVo> list = imcContractService.selectImcContractList(contractForm);
        ExcelUtil<ContractVo> util = new ExcelUtil<>(ContractVo.class);
        return util.exportExcel(list, "协议列表数据");
    }

    /**
     * 协议管理-查询列表
     */
    @ApiOperation("协议管理-查询列表")
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(ContractForm contractForm) {
        Date contractStartDate = contractForm.getContractStartDate();
        Date contractEndDate = contractForm.getContractEndDate();
        if (contractStartDate != null && contractEndDate != null && contractEndDate.compareTo(contractStartDate) < 0) {
            throw new BusinessException("截止日期不能小于开始日期");
        }
        List<ContractVo> list = imcContractService.selectImcContractList(contractForm);
        return getDataTable(list);
    }
}
